{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Document('/home/tom/my_learn/my_danwen/0_playgrounds/0_test_data/account.pdf')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import fitz  # PyMuPDF 的主模块\n",
    "\n",
    "# 打开一个 PDF 文件\n",
    "pdf_path = \"/home/tom/my_learn/my_danwen/0_playgrounds/0_test_data/account.pdf\"  # 替换为你的 PDF 文件路径\n",
    "doc = fitz.open(pdf_path)\n",
    "doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "doc.page_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "page 0 of /home/tom/my_learn/my_danwen/0_playgrounds/0_test_data/account.pdf"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "page=doc[0]\n",
    "page"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1页 共16页\n",
      "专题三：会计的三大工具\n",
      "会计凭证\n",
      "会计凭证既是记录经济业务发生或者完成情况的书面证明，也是登记账簿的依据。\n",
      "原始凭证：在经济业务发生时直接取得或填制的、用以记录经济业务的发生或完成情况的文字\n",
      "凭据\n",
      "【提示 】进行会计核算的重要原始资料，是记账的原始依据\n",
      "1\n",
      "【提示 】是会计资料中最具有法律效力的一种书面证明文件\n",
      "2\n",
      "\n"
     ]
    }
   ],
   "source": [
    "text=page.get_text()\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_info_list = page.get_image_info(xrefs=True)  # 获取页面中所有图像的信息\n",
    "len(image_info_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_info=image_info_list[0]\n",
    "xref = image_info['xref']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "extracted_image = page.parent.extract_image(xref)  # 提取图像数据\n",
    "image_data = extracted_image[\"image\"]  # 获取图像的二进制数据\n",
    "imgrefpath = os.path.join(os.getcwd(), \"vectorstore/image_references\")  # 图像保存路径\n",
    "os.makedirs(imgrefpath, exist_ok=True)  # 创建保存路径目录\n",
    "image_path = os.path.join(imgrefpath, f\"image{xref}-page.png\")  # 图像文件名\n",
    "\n",
    "# 图片上传到minio 文件服务器\n",
    "with open(image_path, \"wb\") as img_file:\n",
    "    img_file.write(image_data)  # 将图像数据写入文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_info_list = page.get_image_info(xrefs=True)  # 获取页面中所有图像的信息\n",
    "page_rect = page.rect  # 获取页面的矩形区域\n",
    "\n",
    "# 遍历页面中的所有图像信息\n",
    "for image_info in image_info_list:\n",
    "    xref = image_info['xref']  # 获取图像的XREF编号\n",
    "    if xref == 0:\n",
    "        continue  # 跳过无效的XREF编号\n",
    "\n",
    "    img_bbox = fitz.Rect(image_info['bbox'])  # 获取图像的边界框\n",
    "    # 过滤掉尺寸过小的图像\n",
    "    if img_bbox.width < page_rect.width / 20 or img_bbox.height < page_rect.height / 20:\n",
    "        continue\n",
    "\n",
    "    extracted_image = page.parent.extract_image(xref)  # 提取图像数据\n",
    "    image_data = extracted_image[\"image\"]  # 获取图像的二进制数据\n",
    "    imgrefpath = os.path.join(os.getcwd(), \"vectorstore/image_references\")  # 图像保存路径\n",
    "    os.makedirs(imgrefpath, exist_ok=True)  # 创建保存路径目录\n",
    "    image_path = os.path.join(imgrefpath, f\"image{xref}-page{pagenum}.png\")  # 图像文件名\n",
    "\n",
    "    # 图片上传到minio 文件服务器\n",
    "    with open(image_path, \"wb\") as img_file:\n",
    "        img_file.write(image_data)  # 将图像数据写入文件"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "danwen",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
